//
//  HLTpinglunTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/10/7.
//  Copyright © 2015年 hlt. All rights reserved.
//

#import "HLTpinglunTableViewController.h"
#import "HLTpinglunTableViewCell.h"
#import "MJExtension.h"
#import "MJRefresh.h"
@interface HLTpinglunTableViewController ()
@property NSUInteger count;

@property NSMutableArray *zhifuma;
@end

@implementation HLTpinglunTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _count=0;
    
    self.zhifuma=[[NSMutableArray alloc]initWithCapacity:10];
    [self setUpTableView];
    
    //去掉多余分割线的方法
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [self.tableView setTableFooterView:view];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}




-(void)setUpTableView{
    //添加下拉的动画图片
    //设置下拉刷新回调
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
    
//    //设置普通状态的动画图片
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [idleImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    //设置即将刷新状态的动画图片
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    //设置正在刷新是的动画图片
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
    
    //上拉刷新
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //隐藏状态文字
    //    self.tableView.footer.stateHidden = YES;
    //设置正在刷新的动画
//    self.tableView.gifFooter.refreshingImages = refreshingImages;
    
    
    
    
}



-(void)getFirstPageData{
    _count = 0;
    [self refreshData];
}

-(void)refreshData{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        
        [self getzhifumalist:[NSString stringWithFormat: @"%ld", (long)_type] page:@"0"];
        
        //        [self getMerchantList];
    });
}

-(void)loadMoreData{
    NSLog(@"2222  offset:%zd",_count);
    _count = _count + 10;
    NSLog(@"3333  offset:%zd",_count);
    [self refreshData];
}




-(void) getzhifumalist:(NSString *) type page:(NSString *) page
{
//    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    NSInteger page1=_count/10;
    if (page1>0) {
        //获取商家评论（包括回复&shop_id=476&rank=4,1是好评，2是中评，3是差评，4是全部）
        NSString *url=[getpinglun stringByAppendingFormat:@"&shop_id=%@&rank=%@&page=%ld",self.shop_id,type,(long)page1];
        [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
            
            [self.zhifuma addObjectsFromArray:ary];
            
            NSUInteger cunt=[ary count];
            _count=_count-10+cunt;
            
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        } faliure:^(NSString *error) {
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            
        }];
        
        
    }
    else
    {
        NSString *url=[getpinglun stringByAppendingFormat:@"&shop_id=%@&rank=%@&page=%ld",self.shop_id,type,(long)page1];
        [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
            
            NSUInteger cunt=[ary count];
            _count=cunt;
            [self.zhifuma removeAllObjects];
            [self.zhifuma addObjectsFromArray:ary];
                 
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        } faliure:^(NSString *error) {
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            
        }];
        
        
    }
    
    
    
}



- (void) reloadtable
{
    _count=0;
    [self getzhifumalist:[NSString stringWithFormat: @"%ld", (long)_type] page:@"0"];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.zhifuma.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //    static NSString *identify1 = @"HLTloginTableViewCell";
    static NSString *identify1 = @"HLTpinglunTableViewCell";
    
    //    HLTloginTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
    HLTpinglunTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identify1];
    if(cell == nil)
    {
        //        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTloginTableViewCell" owner:nil options:nil] firstObject];
        cell= [[[NSBundle mainBundle]loadNibNamed:@"HLTpinglunTableViewCell" owner:nil options:nil] firstObject];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
//    [a substringWithRange:NSMakeRange(4,2)]
    NSString *str1=[self.zhifuma[indexPath.row][@"user_name"] substringWithRange:NSMakeRange(0,3)];//截取下标2之前的字符串
    NSString *str2=[self.zhifuma[indexPath.row][@"user_name"] substringWithRange:NSMakeRange(8,3)];//截取下标7之后的字符串
    
    
    NSString *str3=[NSString stringWithFormat:@"%@****%@",str1,str2];
    
    
    cell.username.text=str3;
    
    cell.dengji.show_star=[self.zhifuma[indexPath.row][@"comment_rank"] integerValue];
    cell.date.text=self.zhifuma[indexPath.row][@"at1"];
    cell.pingluninfo.text=self.zhifuma[indexPath.row][@"ct1"];
    cell.username.text=str3;
    if ([self.zhifuma[indexPath.row][@"ct2"] isEqual:@"00"]) {
        //没有回复
        cell.shangjiahuifuview.hidden=YES;
        
    }
    else
    {
        cell.huifuinfo.text=self.zhifuma[indexPath.row][@"ct2"];
      
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:14];
    NSString *titleContent = self.zhifuma[indexPath.row][@"ct1"];
    titleLabel.text = titleContent;
    titleLabel.numberOfLines = 0;//多行显示，计算高度
    titleLabel.textColor = [UIColor lightGrayColor];
    CGSize titleSize = [titleContent boundingRectWithSize:CGSizeMake(screen_width-40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    titleLabel.size = titleSize;
    
    if ([self.zhifuma[indexPath.row][@"ct2"] isEqual:@"00"]) {
        //没有回复
        return 68+titleSize.height;
        
    }
    else
    {
        NSString *titleContent = self.zhifuma[indexPath.row][@"ct2"];
        titleLabel.text = titleContent;
         CGSize titleSize1 = [titleContent boundingRectWithSize:CGSizeMake(screen_width-50, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
        return 56+titleSize.height+titleSize1.height+19+10;
    }
    
    
}






@end
